//
//  MovieDetailViewController.m
//  豆瓣电影
//
//  Created by dllo on 16/6/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailModel.h"
#import "MovieDetailTableViewCell.h"

@interface MovieDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieDetailArray;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) MovieDetailModel *movieDetailModel;

@end

@implementation MovieDetailViewController

-(void)dealloc
{
    [_movieDetailModel release];
    [_movieDetailArray release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    
    [self handleData];
    
    [self createTableView];
    
    [self createNavegationItem];

}

-(void)config
{
    self.movieDetailArray = [NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.title = self.nameText;
    
    self.navigationItem.hidesBackButton = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)createNavegationItem
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_back@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_share@2x.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
}

-(void)backAction:(UINavigationItem *)item
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)handleData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"m%@",self.idText] ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSDictionary *dic = [dictionary objectForKey:@"result"];
    
    self.movieDetailModel = [[MovieDetailModel alloc]init];
    
    [self.movieDetailModel setValuesForKeysWithDictionary:dic];
    
//    NSLog(@"%@", self.movieDetailModel);
}

-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 100) style:UITableViewStylePlain];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 200;
    [self.tableView registerClass:[MovieDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
        
    cell.movieDetailModel = self.movieDetailModel;
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height;
}

@end










